package definite.Guide.Book.JRadioButtonMenuItem;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;

public class RadioButtonSample {

	static Icon three = new ImageIcon("one.gif");
	static Icon four = new ImageIcon("one.gif");
	static Icon five = new ImageIcon("one.gif");
	static Icon six = new ImageIcon("one.gif");

	public static class ButtonActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			AbstractButton button = (AbstractButton) e.getSource();
			ButtonModel model = button.getModel();
			boolean isSelect = model.isSelected();
			System.out.println(e.getActionCommand() + " is selected ? "
					+ isSelect);
		}

	}

	public static class ButtonItemListener implements ItemListener {

		@Override
		public void itemStateChanged(ItemEvent e) {
			AbstractButton button = (AbstractButton) e.getSource();
			int state = e.getStateChange();
			String select = (state == ItemEvent.SELECTED) ? "SELECTED"
					: "UNSELECTED";
			System.out.println(button.getText() + " is selected ? " + select);
		}

	}

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {
				final ActionListener actionListener = new ButtonActionListener();
				final ItemListener itemListener = new ButtonItemListener();

				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("JRadioButonMenuItem");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				// declare components
				JMenuBar menuBar = new JMenuBar();
				JMenu menu = new JMenu("Menu");
				menu.setMnemonic(KeyEvent.VK_M);
				ButtonGroup group = new ButtonGroup();

				JRadioButtonMenuItem empty = new JRadioButtonMenuItem();
				empty.setActionCommand("Empty");
				empty.addActionListener(actionListener);
				group.add(empty);
				menu.add(empty);

				JRadioButtonMenuItem oneMenuItem = new JRadioButtonMenuItem(
						"Partride");
				oneMenuItem.addActionListener(actionListener);
				group.add(oneMenuItem);
				menu.add(oneMenuItem);

				JRadioButtonMenuItem towMenuItem = new JRadioButtonMenuItem(
						"Turtle Dove", true);
				towMenuItem.addActionListener(actionListener);
				group.add(towMenuItem);
				menu.add(towMenuItem);

				JRadioButtonMenuItem threeMenuItem = new JRadioButtonMenuItem(
						"France Hens", three);
				threeMenuItem.addActionListener(actionListener);
				group.add(threeMenuItem);
				menu.add(threeMenuItem);

				JRadioButtonMenuItem fourMenuItem = new JRadioButtonMenuItem(
						"Four Item", four, true);
				fourMenuItem.addActionListener(actionListener);
				group.add(fourMenuItem);
				menu.add(fourMenuItem);

				JRadioButtonMenuItem fiveMenuItem = new JRadioButtonMenuItem(
						five);
				fiveMenuItem.addActionListener(actionListener);
				group.add(fiveMenuItem);
				menu.add(fiveMenuItem);

				JRadioButtonMenuItem sixMenuItem = new JRadioButtonMenuItem(
						six, true);
				sixMenuItem.addActionListener(actionListener);
				group.add(sixMenuItem);
				menu.add(sixMenuItem);

				menuBar.add(menu);

				frame.setJMenuBar(menuBar);
				frame.setLocationRelativeTo(null);
				frame.setSize(350, 250);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}

}
